using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using libsecondlife;

namespace IMResponder
{
    public partial class frmIMResponder : Form
    {
        SecondLife Client = new SecondLife();
        string Message = String.Empty;

        public frmIMResponder()
        {
            InitializeComponent();

            Client.Self.OnInstantMessage += new InstantMessageCallback(OnInstantMessage);
        }

        void OnInstantMessage(LLUUID fromAgentID, string fromAgentName, LLUUID toAgentID, 
            uint parentEstateID, LLUUID regionID, LLVector3 position, byte dialog, bool groupIM, 
            LLUUID imSessionID, DateTime timestamp, string message, byte offline, 
            byte[] binaryBucket)
        {
            if (dialog == (byte)InstantMessageDialog.MessageFromObject && message == "!quit")
            {
                string replyMessage = "Hi! I was sitting in the sim " + Client.Network.CurrentSim.Region.Name +
                    " at " + Client.Self.Position.ToString() + " when an object of yours named " + fromAgentName + 
                    " sent me this instant message: " + message +
                    ". Not only is this message useless for stopping any bots from running (such as myself), it is " +
                    "likely annoying real people who visit your land or live nearby. You will receive one of these " +
                    "messages every time an object of yours sends the !quit message to me, I hope you can see how " +
                    "annoying automated IMs can be.";
                Client.Self.InstantMessage(fromAgentID, replyMessage);

                lock (Message)
                {
                    Message = "Warned " + fromAgentID.ToString() + " about object " + fromAgentName;
                    Invoke(new MethodInvoker(LogMessage));
                }
            }
            else if (dialog == (byte)InstantMessageDialog.RequestTeleport)
            {
                lock (Message)
                {
                    Message = "Accepting a teleport lure from " + fromAgentName;
                    Invoke(new MethodInvoker(LogMessage));
                }

                Client.Self.TeleportLureRespond(fromAgentID, true);
            }
        }

        private void LogMessage()
        {
            txtLog.AppendText(Message + Environment.NewLine);
        }

        private void cmdConnect_Click(object sender, EventArgs e)
        {
            grpLogin.Enabled = false;

            if (cmdConnect.Text == "Connect")
            {
                cmdConnect.Text = "Disconnect";
                grpLogin.Enabled = true;

                if (Client.Network.Login(txtFirstName.Text, txtLastName.Text, txtPassword.Text,
                    "IMResponder", "John Hurliman <jhurliman@wsu.edu>"))
                {
                    txtLog.AppendText("Login successful" + Environment.NewLine);

                    txtFirstName.Enabled = false;
                    txtLastName.Enabled = false;
                    txtPassword.Enabled = false;
                }
                else
                {
                    txtLog.AppendText("Login failed: " + Client.Network.LoginError + Environment.NewLine);
                }
            }
            else
            {
                cmdConnect.Text = "Connect";
                Client.Network.Logout();

                txtFirstName.Enabled = true;
                txtLastName.Enabled = true;
                txtPassword.Enabled = true;
                grpLogin.Enabled = true;
            }
        }
    }
}
